package com.zhaoxi.project.sys.controller;

import java.util.List;

import jakarta.servlet.http.HttpServletResponse;

import com.zhaoxi.project.sys.service.impl.SysDictTypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhaoxi.common.utils.poi.ExcelUtil;
import com.zhaoxi.framework.aspectj.lang.annotation.Log;
import com.zhaoxi.framework.aspectj.lang.enums.BusinessType;
import com.zhaoxi.framework.web.BaseService;
import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.framework.web.page.TableDataInfo;
import com.zhaoxi.project.sys.domain.SysDictType;

/**
 * 数据字典信息
 *
 * @author zhaoxi
 */
@RestController
@RequestMapping("/sys/dict/type")
public class SysDictTypeController extends BaseService {
    @Autowired
    private SysDictTypeServiceImpl dictTypeService;

    /**
     * 1、字典类型管理——字典类型列表：通过租户id查询字典类型列表（xieleilei 12.6）
     */
    @PreAuthorize("@ss.hasPermi('sys:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDictType dictType) {
        startPage();
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        return getDataTable(list);
    }

    /**
     * 2、字典类型管理——查询字典类型的详细信息（xieleilei 12.6）
     */
    @PreAuthorize("@ss.hasPermi('sys:dict:query')")
    @GetMapping(value = "/{dictId}")
    public PageData getInfo(@PathVariable Long dictId) {
        return success(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 2-1、字典类型管理——查询字典类型的详细信息
     */
    /*@PreAuthorize("@ss.hasPermi('sys:dict:query')")
    @GetMapping(value = "/{dictType}")
    public AjaxResult getInfo(@PathVariable SysDictData dictType) {
        return success(dictTypeService.selectDictDataByType(dictType));
    }*/

    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('sys:dict:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictType dictType) {
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        util.exportExcel(response, list, "字典类型");
    }

    /**
     * 3、字典类型管理——字典类型下拉框：条件搜索区域，通过租户id获取字典类型下拉框（xieleilei 12.6）
     */
    @GetMapping("/optionselect")
    public PageData optionselect(SysDictType dictType) {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll(dictType);
        return success(dictTypes);
    }


    /**
     * 4、字典类型管理——新增字典类型：保证当前租户字典类型唯一，并且存入缓存（xieleilei 12.6）
     */
    @PreAuthorize("@ss.hasPermi('sys:dict:add')")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public PageData add(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return toAjax(dictTypeService.insertDictType(dict));
    }

    /**
     * 5、字典类型管理——修改字典类型（xieleilei 12.6）
     */
    @PreAuthorize("@ss.hasPermi('sys:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public PageData edit(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return toAjax(dictTypeService.updateDictType(dict));
    }

    /**
     * 6、删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('sys:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public PageData remove(@PathVariable Long[] dictIds) {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return success();
    }

    /**
     * 7、刷新字典缓存
     */
    @PreAuthorize("@ss.hasPermi('sys:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public PageData refreshCache() {
        dictTypeService.resetDictCache();
        return success();
    }

}
